package com.tedu.food.sys.service.impl;

import com.tedu.food.common.util.ShiroUtils;
import com.tedu.food.common.vo.CartVo;
import com.tedu.food.sys.dao.CartItemDao;
import com.tedu.food.sys.dao.GoodsDao;
import com.tedu.food.sys.entity.CartItem;
import com.tedu.food.sys.entity.Goods;
import com.tedu.food.sys.entity.Member;
import com.tedu.food.sys.service.CartItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CartItemServiceImpl implements CartItemService{
	@Autowired
	private CartItemDao cartItemDao;
	@Autowired
	private GoodsDao goodsDao;

	/**
	 * 获取用户的购物车状态为1可用的商品总数量和总价
	 */
	@Override
	public CartVo findCartInfo() {
		Member user = ShiroUtils.getUser();
		List<CartItem> cartItem = cartItemDao.findCartItemsBystatus(1, user.getId());
		if(cartItem == null || cartItem.size() == 0)//购物车没有可用的商品
			return new CartVo();
		int number = 0;
		double price = 0.0;
		for(CartItem item : cartItem) {
			int num = item.getNumber();
			double pri= item.getPrice();
			price += pri * num;
			number += num;
		}
		return new CartVo(number, price);
	}

	@Override
	public List<CartItem> findCartItemsByUserId() {
		Member user = ShiroUtils.getUser();
		return cartItemDao.findCartItemsBystatus(null, user.getId());
	}

	/** 购物车商品+1, 来自商品列表的+ */
	@Override
	public Integer addCartItem(Integer goodsId) {
		Member user = ShiroUtils.getUser();
		Integer[] goodsIds = {goodsId};
		List<CartItem> cartItemList = cartItemDao.findCartItemsByGoodsIds(goodsIds, user.getId());
		Integer cartId = cartItemDao.findCartIdByUserId(user.getId());//查找该用户购物车id
		Integer rows = 0;
		if(cartItemList == null || cartItemList.size() == 0){//购物车没有该商品信息
			Goods goods = goodsDao.findGoodsById(goodsId);
			CartItem entity = new CartItem();
			entity.setCartId(cartId);
			entity.setGoodsId(goodsId);
			entity.setGoodsName(goods.getGoodsName());
			entity.setGoodsLogo(goods.getGoodsLogo());
			entity.setPrice(goods.getGoodsPrice());
			rows = cartItemDao.insertCartItem(entity);
		}else{ //购物车存在该商品,+1
			CartItem entity = new CartItem();
			entity.setCartId(cartId);
			entity.setGoodsId(goodsId);
			rows = cartItemDao.updateCartItemNumber(1, entity);
		}
		return rows;
	}

	/** 购物车商品-1, 来自商品列表的- */
	@Override
	public Integer subtractCartItem(Integer goodsId) {
		Member user = ShiroUtils.getUser();
		Integer cartId = cartItemDao.findCartIdByUserId(user.getId());//查找该用户购物车id
		CartItem entity = new CartItem();
		entity.setCartId(cartId);
		entity.setGoodsId(goodsId);
		Integer rows = cartItemDao.updateCartItemNumber(0, entity);
		Integer[] goodsIds = {goodsId};
		List<CartItem> cartItemList = cartItemDao.findCartItemsByGoodsIds(goodsIds, user.getId());
		if(cartItemList.get(0).getNumber() == 0){//购物车该商品数量减到0
			Integer r = cartItemDao.deleteCartItemsByGoodsIds(goodsIds, user.getId());//删除购物车该商品
		}
		return rows;
	}

	/** 购物车商品+1, 来自购物车列表的+ */
	@Override
	public Integer addCartItemById(Integer id) {
		Integer rows = cartItemDao.updateCartItemNumberById(1, id);
		return rows;
	}
	/** 购物车商品-1, 来自购物车列表的- */
	@Override
	public Integer subtractCartItemById(Integer id) {
		Integer rows = cartItemDao.updateCartItemNumberById(0, id);
		CartItem cartItem = cartItemDao.findCartItemById(id);
		if(cartItem.getNumber() == 0)
			cartItemDao.deleteCartItemById(id);
		return rows;
	}

}
